package com.qipay.api.home.controller;

import com.qipay.baas.model.Order;
import com.qipay.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;



/**
 * Created by dong on 2016/6/13.
 */
@RestController
public class AjaxController {
    @Autowired
    OrderService orderService;

    @RequestMapping(value = "/ajax", method = RequestMethod.GET, produces = "application/json")
    public int ajaxDo(@RequestParam(value = "n", required = false, defaultValue = "") String methodtype,
                      @RequestParam(value = "orderNo", required = false, defaultValue = "") String orderNo
                    ) throws UnsupportedEncodingException {
        if (methodtype != null && methodtype.length() > 0 && orderNo.length() > 0) {
            switch (methodtype) {
                case "findorderstatus":
                    int status = findOrderStatus(orderNo);
                    return status;
                default:
                    return 0;
            }
        }
        return -1;
    }

    private int findOrderStatus(String orderNo) {
        Order order = orderService.getByTradeNo(orderNo);
        if (order!=null&&order.getState().toString().equals("3")&&order.getFinishTime() !=null) {
            return 1;
        }
        return 0;
    }





}
